package com.zhuannimei.utility;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.json.JSONException;
import org.json.JSONObject;

public class JsonUtil {

	private static final int BITS_PER_UNIT = 8;

	public static String mapToJson(Map<String, String> map) {
		if (map == null) { // map为空的检测。 nobody 20121221
			return "map is null";
		}
		JSONObject job = new JSONObject();
		Set<String> set = map.keySet();
		for (String ss : set) {
			try {
				job.put(ss, map.get(ss));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return job.toString();
	}

	/**
	 * 获取访问参数集
	 * 
	 * @param map
	 * @return
	 */
	public static Map<String, String> getJsonMap(Map<String, String> map) {
		Map<String, String> map2 = new HashMap<String, String>();
		String value = mapToJson(map);
		map2.put("data", value);
		return map2;
	}
	
	private static int position(int idx){//bits big-endian in each unit
		return 1<<(BITS_PER_UNIT-1-(idx%BITS_PER_UNIT));
	}
}
